import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-08-31
 * Time: 17:39
 */
class Solution4 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        // 1.哈希统计 nums[i] i
        Map<Integer,Integer> hash = new HashMap<>();

        for(int i = 0; i < nums.length; i++) {
            if(hash.containsKey(nums[i])) {
                if(i - hash.get(nums[i]) <= k) {
                    return true;
                }
            }
            hash.put(nums[i],i);
        }
        return false;
    }
}
